<?php
/**
 * 样衣采购需求
 * @author sundongdong
 * @date 2016-07-19
 */
class Purchase_SampleClothController extends Controller_NeedLogin {
	/**
	 * 初始化
	 */
	public function init() {
		parent::init();
		$this->model = new Purchase_Model_SampleCloth();
	}
	/**
	 * 样衣需求列表
	 */
	public function sampleAction() {
		// 接受查询参数
		$get_data = $this->getRequest()->getParams();
		
		$filter = array();
		$filter['page'] = isset($get_data['page']) ? $get_data['page'] : 1;
		$filter['perPage'] = isset($get_data['page_size']) ? $get_data['page_size'] : $this->page_size;
		$filter['key'] = isset($get_data['key']) ? $get_data['key'] : '';
		$filter['value'] = isset($get_data['value']) ? $get_data['value'] : '';
		$filter['good_status'] = isset($get_data['good_status']) ? $get_data['good_status'] : '';
		$filter['start'] = isset($get_data['start']) ? $get_data['start'] : '';
		$filter['end'] = isset($get_data['end']) ? $get_data['end'] : '';
		$filter['sync_status'] = isset($get_data['sync_status']) ? $get_data['sync_status'] : '';
		$filter['privide_status'] = isset($get_data['privide_status']) ? $get_data['privide_status'] : '';
		$filter['is_extended'] = isset($get_data['is_extended']) ? $get_data['is_extended'] : '';
		$filter['sample_type'] = isset($get_data['sample_type']) ? $get_data['sample_type'] : '';
		
		$data = $this->model->getSampleList($filter);
		$page = $this->page($data['count'], $filter['perPage']);
		
		$privide_status = Helper_Config::get('sample', 'privide_status', 'status');
		$sample_type = Helper_Config::get('sample','sample_type','type');
		$sync_status = Helper_Config::get('sample', 'sync_status', 'status');
		$good_status = Helper_Config::get('sample', 'good_status', 'status');
		
		$this->smarty->assign('sample_type', $sample_type);
		$this->smarty->assign('privide_status', $privide_status);
		$this->smarty->assign('sync_status', $sync_status);
		$this->smarty->assign('good_status', $good_status);
		$this->smarty->assign('filter', $filter);
		$this->smarty->assign('list', $data['list']);
		$this->smarty->assign('page_link', $page);
		$this->smarty->assign('pre_img', $this->config['image']['domain']);
		
		$this->smarty->display("sample_list.tpl");
	}
	/**
	 * 样衣推广跟踪表
	 */
	public function fbAction() {	// 接受查询参数
		$get_data = $this->getRequest()->getParams();
		
		$filter = array();
		$filter['page'] = isset($get_data['page']) ? $get_data['page'] : 1;
		$filter['perPage'] = isset($get_data['page_size']) ? $get_data['page_size'] : $this->page_size;
		$filter['sku'] = isset($get_data['sku']) ? $get_data['sku'] : '';
		$filter['operate_user'] = isset($get_data['operate_user']) ? $get_data['operate_user'] : '';
		$filter['time_type'] = isset($get_data['time_type']) ? $get_data['time_type'] : '';
		$filter['start'] = isset($get_data['start']) ? $get_data['start'] : '';
		$filter['end'] = isset($get_data['end']) ? $get_data['end'] : '';
		$filter['fb_feedback'] = isset($get_data['fb_feedback']) ? $get_data['fb_feedback'] : '';
		$filter['is_extended'] = isset($get_data['is_extended']) ? $get_data['is_extended'] : '';
		$filter['lose_stock_min'] = isset($get_data['lose_stock_min']) ? $get_data['lose_stock_min'] : '';
		$filter['lose_stock_max'] = isset($get_data['lose_stock_max']) ? $get_data['lose_stock_max'] : '';
		$filter['seven_sale_min'] = isset($get_data['seven_sale_min']) ? $get_data['seven_sale_min'] : '';
		$filter['seven_sale_max'] = isset($get_data['seven_sale_max']) ? $get_data['seven_sale_max'] : '';
		$filter['rise_rate_min'] = isset($get_data['rise_rate_min']) ? $get_data['rise_rate_min'] : '';
		$filter['rise_rate_max'] = isset($get_data['rise_rate_max']) ? $get_data['rise_rate_max'] : '';
		
		$data = $this->model->getFbList($filter);
		$page = $this->page($data['count'], $filter['perPage']);
		
		$sample_type = Helper_Config::get('sample','sample_type','type');
		$time_type = Helper_Config::get('sample', 'time_type', 'type');
		$fb_feedback = Helper_Config::get('sample', 'fb_feedback', 'status');
		$fb_operation = Helper_Config::get('sample', 'fb_operation', 'type');
		
		$this->smarty->assign('filter', $filter);
		$this->smarty->assign('sample_type', $sample_type);
		$this->smarty->assign('time_type', $time_type);
		$this->smarty->assign('fb_feedback', $fb_feedback);
		$this->smarty->assign('fb_operation', $fb_operation);
		
		$this->smarty->assign('list', $data['list']);
		$this->smarty->assign('page_link', $page);
		$this->smarty->assign('pre_img', $this->config['image']['domain']);

		$this->smarty->display("sample_fb_list.tpl");
	}
	/**
	 * 添加样衣采购需求
	 */
	public function addAction() {
		// 获取样衣类型配置
		$sample_type = Helper_Config::get('sample','sample_type','type');
		$this->smarty->assign('type', $sample_type);
		
		$this->smarty->display("sample_add.tpl");
	}
	/**
	 * 添加样衣采购需求处理
	 */
	public function addDealAction() {
		$data = $this->getRequest()->getParams();
		$sku = $data['valid_sku'];
		$num = $data['num'];
		$remark = $data['remark'];
		$process_same_sku = $data['process_same_sku'];
		$sample_type = $data['sample_type'];
		// 消息提示
		$msg = '';
		$type_msg = '';
		// 是否是曝光款
		$is_sample=false;
		if ($sku) {
			$sku = explode(',', $sku);
			// 曝光款
			if ($sample_type == 1) {
				$is_sample = true;
				$pre_sku = array();
				foreach ($sku as $gsn) {
					// 检查产品审核和上架信息
					$goods_info = $this->model->getProductBySku($gsn);
					if (empty($this->is_cloth_category($gsn))){
						$type_msg .= $gsn.'sku不是服装类产品，不能添加曝光款!<br>';
						continue;
					}
					if ($goods_info && ($goods_info['online_status'] == 2 || $goods_info['edit_status'] == 10)) {
						if (!in_array(substr($gsn, 0, 7), $pre_sku)) {
							$pre_sku[] = substr($gsn, 0, 7);
						}
					} else {
						$type_msg .= $gsn.'sku不存在或未上架或非\'审核通过（暂不上架)\'类产品，不能添加曝光款!<br>';
					}
				}
				if (count($pre_sku) > 0) {
					foreach ($pre_sku as $val) {
						$ex = $this->model->existFb($val);
						if ($ex) {
							$type_msg .= $val.'FB推广曝光款已存在!<br/>';
						} else {
							// 添加
							$fb = array(
								'sku' => $val,
								'add_time' => date("Y-m-d H:i:s"),
								'add_user' => $this->_session->user_name
							);
							$this->model->addFb($fb);
							$type_msg .= $val . "FB推广曝光款添加成功<br>";
						}
					}
				} else {
					$type_msg .= "无推广曝光款添加，请检查<br>";
				}
			}
			// 同款采购
			if ($process_same_sku) {
				$new_skus = array();
				$sub_skus = array();
				foreach ($sku as $sn) {
					if (!in_array(substr($sn, 0, 7), $sub_skus)) {
						$new_skus[] = $sn;
					}
					$sub_skus[] = substr($sn, 0, 7);
				}
			} else {
				$new_skus = $sku;
			}
			foreach ($new_skus as $new_sn) {
				if (empty($this->is_cloth_category($new_sn))) {
					$msg .= $new_sn . "该产品不是服装类，不能添加样衣需求<br/>";
					continue;
				}
				// 检查重复
				if ($process_same_sku) {
					$sub_sn = substr($new_sn, 0, 7);
					$has_exist = $this->model->existSample($sub_sn, true);
				} else {
					$has_exist = $this->model->existSample($new_sn);
				}
				if ($has_exist) {
					$msg .= $new_sn.'重复<br/>';
				} else {
					if ($process_same_sku) {
						$all_same = $this->model->getAllSame($sub_sn);
					}
					$all_same[$new_sn] = array('goods_sn'=>$new_sn);
					foreach ($all_same as $sn_arr) {
						$sn = $sn_arr['goods_sn'];
						$goods_info = $this->model->getProductInfo($sn);
						if ($goods_info && ($goods_info['online_status'] == 2 || $goods_info['edit_status'] == 8)) {
							$same = array(
								'sku' => $sn,
								'img' => $goods_info['thumb_url'],
								'goods_name' => $goods_info['product_name'],
								'price' => $goods_info['purchase_price_base'],
								'num' => $num,
								'goods_status' => $goods_info['goods_resource_status'],
								'purchase_type' => 1,
								'warehouse_id' => '4',
								'remark' => $remark,
								'add_user' => $this->_session->user_name,
								'add_time' => date('Y-m-d H:i:s'),
								'purchaser'=> $goods_info['purchaser'],
								'supplier_sn' => $goods_info['supplier_sn'],
								'supplier_name' => $goods_info['supplier_name'],
								'sample_type' => $sample_type
							);
							$this->model->addSample($same);
							$msg .= $sn.'添加成功<br/>';
						} else {
							$msg .= $sn.'sku不存在或未上架或非\'审核通过（暂不上架)\'类产品<br/>';
						}
					}
				}
			}
		} else {
			$msg = 'sku为空';
		}
		exit(json_encode(array('status'=>1, 'msg'=>$msg.$type_msg)));
	}
	// 根据sku判断是否是服装类
	private function is_cloth_category($goods_sn) {
		// 服装分类数据
		$allow_cate = Helper_Config::get('sample', 'sample_category', 'category');
		$allowCat = array_keys($allow_cate);
		// 该产品所属的分类
		$parentCatStr = $this->model->getProductCate($goods_sn);
		$parentCatArr = explode(',',$parentCatStr);
		$intersection = array_intersect($parentCatArr, $allowCat);
		return $intersection;
	}
	/**
	 * 获取产品名称
	 */
	public function getNameAction() {
		$skus = $this->getRequest()->getParam('skus');
		$res = $this->model->getProductNames(explode(',', $skus));
		exit(json_encode($res));
	}
	/**
	 * 样衣推广，支持批量
	 */
	public function extendSampleAction() {
		$ids = $this->getRequest()->getParam('id');
		if (!$ids) {
			exit(json_encode(array('opr'=>0,'content'=>'error id')));
		}
		$res = $this->model->extendSample($ids, $this->_session->user_name);
		echo json_encode($res);
	}
	/**
	 * 跟踪表推广
	 */
	public function extendAction() {
		$id = $this->getRequest()->getParam('id');
		if (!$id) {
			exit(json_encode(array('opr'=>0,'content'=>'error id')));
		}
		$res = $this->model->extendFb($id, $this->_session->user_name);
		echo json_encode($res);
	}
	/**
	 * FB反馈
	 */
	public function feedBackAction() {
		$data = $this->getRequest()->getParams();
		if (!$data['id']) {
			exit(json_encode(array('opr'=>0,'content'=>'error id')));
		}
		$res = $this->model->updateFeedBackById($data, $this->_session->user_name);
		echo json_encode($res);
	}
	/**
	 * 预计回样时间
	 */
	public function returnSampleAction() {
		$data = $this->getRequest()->getParams();
		if (!$data['id']) {
			exit(json_encode(array('opr'=>0,'content'=>'error id')));
		}
		$res = $this->model->updateReturnSampleById($data, $this->_session->user_name);
		echo json_encode($res);
	}
	/**
	 * 预计回大货时间
	 */
	public function expectProAction() {
		$data = $this->getRequest()->getParams();
		if (!$data['id']) {
			exit(json_encode(array('opr'=>0,'content'=>'error id')));
		}
		$res = $this->model->updateExpectProById($data, $this->_session->user_name);
		echo json_encode($res);
	}
	/**
	 * 删除
	 */
	public function delFbAction() {
		$id = $this->getRequest()->getParam('id');
		if (!$id) {
			exit(json_encode(array('opr'=>0,'content'=>'error id')));
		}
		$res = $this->model->deleteFb($id, $this->_session->user_name);
		echo json_encode($res);
	}
	/**
	 * 导出excel
	 */
	public function exportExcelAction() {
		$ids = $this->getRequest()->getParam('id');
		if (!$ids) {
			exit(json_encode(array('opr'=>0,'content'=>'error id')));
		}
		$data = $this->model->getExportSamples($ids);
		
		$filename = "sample_cloth_fb-".date('YmdHis').'.xls';
		$objPHPExcel = new PHPExcel();
		$objPHPExcel->getActiveSheet()->getColumnDimension('A')->setWidth(15);
		$objPHPExcel->getActiveSheet()->getStyle('A1')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
		$objPHPExcel->getActiveSheet()->getStyle('B1')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
				
		$objPHPExcel->getActiveSheet()->setCellValue('A1', '产品编码');
		$objPHPExcel->getActiveSheet()->setCellValue('B1', '产品图片');
		
		foreach ($data as $key => $v) {
			$server = $this->config['image']['domain'] . $v['img'];
			
			$objPHPExcel->getActiveSheet()->getStyle('A'.($key+2))->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
			$objPHPExcel->getActiveSheet()->getStyle('B'.($key+2))->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
			$objPHPExcel->getActiveSheet()->getRowDimension(($key+2))->setRowHeight(30);
			$objPHPExcel->getActiveSheet()->setCellValue('A'.($key+2), $v['sku']);
			$objPHPExcel->getActiveSheet()->setCellValue('B'.($key+2), $server);
		}
		header('Content-Type: application/vnd.ms-excel');
		header('Content-Disposition: attachment;filename="'.$filename.'"');
		header('Cache-Control: max-age=0');
		$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
		$objWriter->save('php://output');
	}
}